//
//  LFPitureViewController.swift
//  选取相册
//
//  Created by 刘健 on 16/4/4.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class LFPitureViewController: UICollectionViewController {
    
      //MARK: -- 属性
    
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    var pictures: [UIImage] = [UIImage]()
    
    let MaxPictCount = 3
    
    var clickButtonIndexPath: NSIndexPath?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init() {
        super.init(collectionViewLayout: self.layout)
        
        collectionView?.backgroundColor = UIColor.lightGrayColor()
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(LFPictureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    
        collectionView?.contentInset = UIEdgeInsetsMake(10, 10, 10, 10)
        
        layout.itemSize = CGSizeMake(80, 80)

    }

    // MARK: UICollectionViewDataSource
  override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
     return pictures.count < MaxPictCount ? pictures.count + 1 : MaxPictCount
    
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LFPictureViewCell
        
        cell.delagate = self
        
        if indexPath.item < pictures.count {
            
            cell.image = pictures[indexPath.item]
        }else {
            
            cell.setupAddButton()
        }
        
       
    
        return cell
    }
}


  //MARK: --LFPictureViewCellDelegate
extension LFPitureViewController: LFPictureViewCellDelegate {
    
    func didClickAddButton(cell: LFPictureViewCell, button: UIButton) {
        
        let picVC = UIImagePickerController()
        
        picVC.delegate = self
        
        //记录下标
        clickButtonIndexPath = collectionView?.indexPathForCell(cell)

        presentViewController(picVC, animated: true, completion: nil)
    }
    
    //删除键
    func didClickDeleteButton(cell: LFPictureViewCell, button: UIButton) {
        
        let indexPath = collectionView?.indexPathForCell(cell)
  
        pictures.removeAtIndex((indexPath?.item)!)
        

        if indexPath?.item == pictures.count {
            collectionView?.reloadData()
        }else {
            
            collectionView?.deleteItemsAtIndexPaths([indexPath!])
        }
 
        
    }
}

extension LFPitureViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        //处理图片过大 优化内存
        
        let Newimage = image.changeImageSize()
   
        //数组加1
        //如果indexPath .item == pictures.count 则是加图片 否则是更换图片
        if  clickButtonIndexPath!.item == pictures.count {
            
            pictures.append(Newimage)
            
        }else {
            
            pictures[(clickButtonIndexPath?.item)!] = Newimage
        }
        
        collectionView?.reloadData()
        
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}


